package com.test.springboot.demo.controller;

import com.github.pagehelper.PageInfo;
import com.test.springboot.demo.pojo.AccountType;
import com.test.springboot.demo.pojo.WechatAccount;
import com.test.springboot.demo.service.AccountService;
import com.test.springboot.demo.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private TypeService typeService;

    @RequestMapping("list")
    public ModelAndView list(String keyword, Integer typeId, String startDate, String endDate, Integer pageNum) {
        System.out.println("=============");
        if (pageNum == null || pageNum < 0) {
            pageNum = 1;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("keyword", keyword);
        param.put("typeId", typeId);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        PageInfo<WechatAccount> pageInfo = accountService.getAccountList(param, pageNum, 2);
        List<AccountType> types = typeService.getTypeList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.addObject("types", types);
        modelAndView.addObject("message", "你好");
        modelAndView.setViewName("list");
        return modelAndView;
    }

    @RequestMapping("go_update")
    public String goUpdate(Integer id, Model model) {
        WechatAccount accont = accountService.getAccountById(id);
        model.addAttribute("account", accont);
        List<AccountType> types = typeService.getTypeList();
        model.addAttribute("types", types);
        return "update";
    }

    @RequestMapping("update")
    public String update(WechatAccount account) {
        accountService.updateAccount(account);
        return "redirect:list";
    }
}
